/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.going.persistencia;

import co.com.going.modelo.Pais;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Olguita
 */
@Stateless
public class DAOpais implements IDAOpais {

    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public void crearPais(Pais pais) {
        
        try {
            
            entityManager.persist(pais);
            
        } catch (Exception e) {
            throw e;
            
        }
    }

    @Override
    public void eliminarPais(Pais pais) {
        
        try {
            System.out.println("Pais a modificar "+pais.toString());
            entityManager.remove(pais);
            
        } catch (RuntimeException e) {
        
             throw e;
            
        }
        
    }

    @Override
    public void modificarPais(Pais pais) {
        
        try {
            
            entityManager.merge(pais);
            
        } catch (RuntimeException e) {
            
           throw e;
            
        }
    }

    @Override
    public List<Pais> paises() {
        
         try {
            
            String Jpql="select c from Pais c";
            Query query=entityManager.createQuery(Jpql);
            return query.getResultList();
            
            
        } catch (RuntimeException e) {
            
            throw e;
        }
    }

    

    @Override
    public Pais buscarPaisPorNombre(String nombre) {
        
        try {
            
            String Jpql="select p from Pais p where p.nombrePais = '"+nombre+"'";
            Query query=entityManager.createQuery(Jpql);
            
            if(query.getResultList().isEmpty()){
            
                return null;
                
            }else{
            
                 return (Pais) query.getResultList().get(0);
           
            }
            
            
        } catch (RuntimeException e) {
            
            throw e;
        }
        
    }

    @Override
    public Pais buscarPaisPorId(Long IDpais) {
        
        try {
            
            return entityManager.find(Pais.class, IDpais);
            
            
        } catch (RuntimeException e) {
            
            throw e;
        }
        
    }
    
    @Override
    public List<Pais> PaisesCBM(){
        
        try{
           
            String JPQL="select p FROM Pais p where p.codPais > 1";
            Query query=entityManager.createQuery(JPQL);
        
             return query.getResultList();
            
        } catch (RuntimeException e) {
            
            throw e;
        }
        
        
        
    } 
    
}
